<?php 
class pay_home 
{ 
	function pay_usertype_add() 
	{ 
		global $mysql_conn,$v_file;
		$user_id = $_COOKIE['user_id'];
		$user_name = $_COOKIE['user_name'];
		$user_type = $_COOKIE['user_type'];
		$user_type_name = $_COOKIE['type_name'];
		$sql = "SELECT * from `user_type` WHERE `type_id` > '5'";
		$result = mysql_query($sql,$mysql_conn);
		while(@$row = mysql_fetch_array($result)) {
			$type_id = $row['type_id'];
			$type_name = $row['type_name'];
			if ($type_id > $user_type) {
				$type_list .= "<option value='".$type_id."' >".$type_name."</option>";
			}
		}
		require($v_file);
	}
	function get_pay_usertype() 
	{ 
		global $mysql_conn,$v_file;
		$type_pay_id = $_POST['type_pay_id'];
		$sql = "SELECT * from `user_type` WHERE `type_id` = '".$type_pay_id."'";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$type_pay = $row['type_pay'];
		echo $type_pay;
	}
	function pay_usertype_add_do() 
	{ 
		global $mysql_conn,$v_file;
		//获取用户基本信息
		$user_id = $_COOKIE['user_id'];
		$user_name = $_COOKIE['user_name'];
		$user_type = $_COOKIE['user_type'];
		$user_type_name = $_COOKIE['type_name'];
		$sql = "SELECT * from `user` WHERE `user_id` = '".$user_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row=mysql_fetch_array($result);
		$user_glod = $row['user_glod'];
		//获取用户购买信息
		$type_pay_id = $_POST['type_pay_id'];
		$type_pay_num = $_POST['type_pay_num'];
		//计算预计花销
		$sql = "SELECT * from `user_type` WHERE `type_id` = '".$type_pay_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row=mysql_fetch_array($result);
		$type_pay = $row['type_pay'];
		$type_name = $row['type_name'];
		$payglod = $type_pay*$type_pay_num;
		if ($payglod > $user_glod) {
			echo '1';
		} else {
			$user_glod_now = $user_glod-$payglod;
			$sql = "SELECT * from `user` WHERE `user_id` = '".$user_id."' ";
			$result = mysql_query($sql,$mysql_conn);
			@$row=mysql_fetch_array($result);
			$user_type_time_end = $row['user_type_time_end'];
			if (empty($user_type_time_end)) {
				$a = date("Y-m-d",time());
				$b = date('Y-m-d',strtotime('+'.$type_pay_num.' Month',strtotime($a)));
			} else {
				$a = $user_type_time_end;
				$b = date('Y-m-d',strtotime('+'.$type_pay_num.' Month',strtotime($a)));
			}
			$log_title = "以单价：".$type_pay." 金币 / 月的价格，购买用户组：".$type_name." 时长：".$type_pay_num." 个月。";
			$sql = "UPDATE `user` SET `user_glod` = '".$user_glod_now."',`user_type_time_end` = '".$b."' WHERE `user_id` =".$user_id.";";
			$result = mysql_query($sql,$mysql_conn);
			@$row = mysql_fetch_array($result);
			$sql="INSERT INTO pay_log (log_id, log_user, log_title, log_type, log_glod) VALUES (NULL,'$user_name','$log_title','消费','$payglod')";
			$result = mysql_query($sql,$mysql_conn);
			@$row = mysql_fetch_array($result);
			echo '2';
		}
	}
	function pay_bot_add() 
	{ 
		global $mysql_conn,$v_file;
		$user_id = $_COOKIE['user_id'];
		$user_name = $_COOKIE['user_name'];
		$user_type = $_COOKIE['user_type'];
		$type_name = $_COOKIE['type_name'];
		$sql = "SELECT * from `user_type` WHERE `type_id` = '".$user_type."'";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$type_pay_bot = $row['type_pay_bot'];
		require($v_file);
	}
	function pay_bot_add_do() 
	{ 
		global $mysql_conn,$v_file;
		$user_id = $_COOKIE['user_id'];
		$user_name = $_COOKIE['user_name'];
		$user_type = $_COOKIE['user_type'];
		$type_name = $_COOKIE['type_name'];
		//购买机器人单价
		$sql = "SELECT * from `user_type` WHERE `type_id` = '".$user_type."'";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$type_pay_bot = $row['type_pay_bot'];
		//预备购买数量
		$type_pay_bot_num = $_POST['type_pay_bot_num'];
		//购买订单总价
		$payglod = $type_pay_bot*$type_pay_bot_num;
		//现有机器人数量和现有金币
		$sql = "SELECT * from `user` WHERE `user_id` = '".$user_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row=mysql_fetch_array($result);
		$user_bot_num = $row['user_bot_num'];
		$user_glod = $row['user_glod'];
		if ($payglod > $user_glod) {
			echo '1';
		} else {
			$user_glod_now = $user_glod-$payglod;
			$user_bot_num_now = $user_bot_num+$type_pay_bot_num;
			$log_title = "以 ".$type_name." 会员组，凭借单价：".$type_pay_bot."金币 / 个的价格，购买机器人 ".$type_pay_bot_num." 个。";
			$sql = "UPDATE `user` SET `user_glod` = '".$user_glod_now."',`user_bot_num` = '".$user_bot_num_now."' WHERE `user_id` =".$user_id.";";
			$result = mysql_query($sql,$mysql_conn);
			@$row = mysql_fetch_array($result);
			$sql="INSERT INTO pay_log (log_id, log_user, log_title, log_type, log_glod) VALUES (NULL,'$user_name','$log_title','消费','$payglod')";
			$result = mysql_query($sql,$mysql_conn);
			@$row = mysql_fetch_array($result);
			echo '2';
		}
		
		
		
	}
	function pay_glod_add() 
	{ 
		global $mysql_conn,$v_file;
		$user_id = $_COOKIE['user_id'];
		$user_name = $_COOKIE['user_name'];
		$user_type = $_COOKIE['user_type'];
		$type_name = $_COOKIE['type_name'];
		$sql = "SELECT * from `pay_set` WHERE `pay_access` = 'alipay' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row=mysql_fetch_array($result);
		$pay_glod_add_access = $row['pay_title'];
		$sql = "SELECT * from `web` WHERE `web_isset` = '1' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row=mysql_fetch_array($result);
		$pay_glod_add_glod = $row['web_glod'];
		require($v_file);
	}
	function pay_glod_add_do() 
	{ 
		global $mysql_conn,$v_file;
		$user_name = $_COOKIE['user_name'];
		$pay_glod_add_money = $_POST['pay_glod_add_money'];
		$pay_glod_add_sn = $_POST['pay_glod_add_sn'];
		$sql="INSERT INTO pay (pay_id, pay_user, pay_sn, pay_money, pay_state) VALUES (NULL,'$user_name','$pay_glod_add_sn','$pay_glod_add_money','0')";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		echo "2";
		
	}
	function home_pay_log() 
	{ 
		global $mysql_conn,$v_file;
		$list_page = $_GET['list_page'];
		$list_num = $_GET['list_num'];
		$sousuokey = $_GET['sousuokey'];
		$user_name = $_COOKIE['user_name'];
		if (empty($list_page)) $list_page = 1;
		if (empty($list_num)) $list_num = 10;
		if ($list_page < 5) {$list_page0 = 1;} else {$list_page0 = $list_page-4;}
		if ($list_page < 5) {$list_page1 = 1;} else {$list_page1 = $list_page-3;}
		if ($list_page < 5) {$list_page2 = 2;} else {$list_page2 = $list_page-2;}
		if ($list_page < 5) {$list_page3 = 3;} else {$list_page3 = $list_page-1;}
		if ($list_page < 5) {$list_page4 = 4;} else {$list_page4 = $list_page;}
		if ($list_page < 5) {$list_page5 = 5;} else {$list_page5 = $list_page+1;}
		if ($list_page < 5) {$list_page6 = 6;} else {$list_page6 = $list_page+2;}
		if ($list_page < 5) {$list_page7 = 7;} else {$list_page7 = $list_page+3;}
		if ($list_page < 5) {$list_page8 = 8;} else {$list_page8 = $list_page+4;}
		$page_btn .= "<button type='button' class='btn btn-white' onClick='list_page_go(".$list_page0.")'><i class='fa fa-chevron-left'></i></button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page1."' onClick='list_page_go(".$list_page1.")'>".$list_page1."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page2."' onClick='list_page_go(".$list_page2.")'>".$list_page2."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page3."' onClick='list_page_go(".$list_page3.")'>".$list_page3."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page4."' onClick='list_page_go(".$list_page4.")'>".$list_page4."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page5."' onClick='list_page_go(".$list_page5.")'>".$list_page5."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page6."' onClick='list_page_go(".$list_page6.")'>".$list_page6."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page7."' onClick='list_page_go(".$list_page7.")'>".$list_page7."</button>";
		$page_btn .= "<button type='button' class='btn btn-white' onClick='list_page_go(".$list_page8.")'><i class='fa fa-chevron-right'></i></button>";
		$list_limit = ($list_page-1)*$list_num.",".$list_num;
		if (empty($sousuokey)) {
			$sql = "SELECT * from `pay_log` WHERE `log_user` = '".$user_name."' limit ".$list_limit;
		} else {
			$sql = "SELECT * from `pay_log` WHERE `log_user` = '".$user_name."' and `log_type` = '".$sousuokey."' ";
		}
		$result = mysql_query($sql,$mysql_conn);
		while(@$row=mysql_fetch_array($result)) {
			$log_id = $row['log_id'];
			$log_user = $row['log_user'];
			$log_title = $row['log_title'];
			$log_type = $row['log_type'];
			$log_glod = $row['log_glod'];
			$serverlist .= "<tr><td>";
			$serverlist .= "    <input type='checkbox' class='i-checks' name='input[]'>";
			$serverlist .= "</td>";
			$serverlist .= "<td>".$log_id."</td>";
			$serverlist .= "<td>".$log_title."</td>";
			$serverlist .= "<td>".$log_type."</td>";
			$serverlist .= "<td>".$log_glod."</td>";
			$serverlist .= "</tr>";
		}
		require($v_file);
	}
} 
/* End of file democontroller.php */ 